<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/25
 * Time: 9:11
 */

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Support\Facades\View;
use Session;
use Request;
use App\Services\ValidateCode;
use Redirect;
class LoginController extends Controller
{

    public function index(){
        return view('home.login.index', []);
    }

    public function imgCode(){
        $code = new ValidateCode();
        $code->doimg('log');
    }

    //登录
    public function login(){

        $data['name'] = trim(Request::input("name"));
        $data['password'] = trim(Request::input("password"));
        $imgCode = trim(Request::input("img_code"));

        if(empty($data['name']))
            return $this->showMsg('0','请输入用户名','/Home/Login/index');

//        if(empty($data['password']))
//            return $this->showMsg('0','请输入密码','/Home/Login/index');

        if(empty($imgCode))
            return $this->showMsg('0','请输入验证码','/Home/Login/index');

        if($imgCode != Session::get('log_img_code'))
            return $this->showMsg('0','验证码不正确','/Home/Login/index');
        $UserSP = new \App\Factories\UserFactory;
        if($UserSP->login($data)){
            //return $this->showMsg('1002','','/Home/Index/index');
            $url = url('/Adm');

            return Redirect::to($url);
        }

        else
            return $this->showMsg('-1002','','/Home/Login/index');

    }

    //登出
    public function logout(){
        $UserSP = new \App\Factories\UserFactory;
        $UserSP->logout();
        $url = url('/');
        return Redirect::to($url);
    }

    //重置密码
    public function resetPage(){
        return view('home.login.reset', []);
    }

    //重置密码
    public function resetPassword(){
        $id = Session::get('user_info')['id'];
        $passwordold = trim(Request::input("passwordold"));
        $password = trim(Request::input("password"));
        $rpassword = trim(Request::input("repeatpassword"));
        $UserSP = new \App\Factories\UserFactory;
        $user_info = $UserSP->getUserInfo(['id'=>$id,'password'=>$passwordold]);
        if(!$user_info)
            return $this->showMsg('0','密码不正确','/Home/Login/resetPage');
        if($password != $rpassword)
            return $this->showMsg('0','密码不一致','/Home/Login/resetPage');
        $res = $UserSP->resetPassword($password,$id);
        if(is_array($res)){
            return $this->showMsg($res['sta'],$res['msg']);
        }
        if($res)
            return $this->showMsg('0','密码修改成功','/Home/Login/index');
        else
            return $this->showMsg('0','密码修改失败','/Home/Login/resetPage');
    }
}